var mysql=require('../../mysql.js');
var util=require('../../utils.js');
var config=require('../../config.js');
var Q = require('q');
var commonCor = require('./commonController.js');

// sql 查询语句
module.exports = function(app){

/*---------------------------------------商品列表------------------------------------------------*/
	// 获得品牌列表
	var goodsList=function(pageNo,pageSize){
		var defer=Q.defer();
		mysql(`select * from goods where g_delete=0&&g_delete=0 limit ${(pageNo-1)*pageSize},${(pageNo*pageSize-1)}`, ((data)=>{
			defer.resolve(data);
		}));
		return  defer.promise;
	};

	/*获得 品牌|分类 列表
		pageNo 当前页
		pageSize 分页数量
	*/
	app.all('/cms/api/goods/getGoodsList', function (req, res) {
		// 检测参数
		if(!req.body.pageNo){
			res.send(util.isScuessCall({
				code:1002,
				desc:'pageNo参数不能为空!',
			}));
			return false;
		} 
		//处理逻辑 
		var pageNo=req.body.pageNo?parseInt(req.body.pageNo):0;
		var pageSize=req.body.pageSize?parseInt(req.body.pageSize):config.pageSize;
		Q.all([commonCor.brands(),commonCor.categorys(),goodsList(pageNo,pageSize)]).then(function(data){
			var brands=data[0];
			var categorys=data[1];
			var goods=data[2];
			
			// 添加品牌和分类中文名
			goods.forEach(function(val,index){
				brands.forEach(function(b_val){
					if(val.b_id===b_val.b_id){
						val.b_name=b_val.b_name;
					}
				});
				categorys.forEach(function(c_val){
					if(val.t_id===c_val.t_id){
						val.t_name=c_val.t_name;
					}
				});
			});
			// 获得总条数
			mysql(`select count(*) as totalCount from goods where g_delete=0`, ((data)=>{
				console.log(data);
				if(data&&data.length){
					res.send(util.isScuessCall({
						data:goods,
						pageNo:pageNo++,
						totalCount:data[0].totalCount
					}));
				};
			}));
		});
	});	
	
	/* 上下架
	    shelves  上下架状态
	    goodsId  商品id
	*/
	app.all('/cms/api/goods/setShelves',((req, res)=>{
		let shelves=req.body.shelves
		let goodsId=req.body.goodsId
		if(!shelves||!goodsId){
			res.send(util.isScuessCall({
				code:1002,
				desc:'参数错误!',
			}));
			return false;
		};
		mysql(`update goods set g_shelves=${shelves==0?1:0} where g_id=${goodsId}`, ((data)=>{
			res.send(util.isScuessCall({
				data:data
			}));
		}));
	}));

	/*获得回收站数据列表
	*/
	var deleteGoodsList=function(pageNo,pageSize){
		var defer=Q.defer();
		mysql(`select * from goods where g_delete=1 limit ${(pageNo-1)*pageSize},${(pageNo*pageSize-1)}`, ((data)=>{
			defer.resolve(data);
		}));
		return  defer.promise;
	};
	app.all('/cms/api/goods/getRecycleBinList', function (req, res) {
		// 检测参数
		if(!req.body.pageNo){
			res.send(util.isScuessCall({
				code:1002,
				desc:'pageNo参数不能为空!',
			}));
			return false;
		} 
		//处理逻辑 
		var pageNo=req.body.pageNo?parseInt(req.body.pageNo):0;
		var pageSize=req.body.pageSize?parseInt(req.body.pageSize):config.pageSize;
		Q.all([commonCor.brands(),commonCor.categorys(),deleteGoodsList(pageNo,pageSize)]).then(function(data){
			var brands=data[0];
			var categorys=data[1];
			var goods=data[2];
			
			// 添加品牌和分类中文名
			goods.forEach(function(val,index){
				brands.forEach(function(b_val){
					if(val.b_id===b_val.b_id){
						val.b_name=b_val.b_name;
					}
				});
				categorys.forEach(function(c_val){
					if(val.t_id===c_val.t_id){
						val.t_name=c_val.t_name;
					}
				});
			});
			// 获得总条数
			mysql(`select count(*) as totalCount from goods where g_delete=1`, ((data)=>{
				if(data&&data.length){
					res.send(util.isScuessCall({
						data:goods,
						pageNo:pageNo++,
						totalCount:data[0].totalCount
					}));
				};
			}));
		});
	});

	/*回收商品
	
	 */
	app.all('/cms/api/goods/setRecycleBin',((req, res)=>{
		let gdelete=req.body.gdelete
		let goodsId=req.body.goodsId
		if(!gdelete||!goodsId){
			res.send(util.isScuessCall({
				code:1002,
				desc:'参数错误!',
			}));
			return false;
		};
		mysql(`update goods set g_delete=${gdelete==0?1:0} where g_id=${goodsId}`, ((data)=>{
			res.send(util.isScuessCall({
				data:data,
			}));
		}));
	}));


	
	
/*---------------------------------------新增|修改 商品------------------------------------------------*/
	/*查询商品信息
	*/
	app.all('/cms/api/goods/selectGoodsById',((req, res)=>{
		let goodsId=req.body.goodsId
		console.log(req.body);
		if(!goodsId){
			res.send(util.isScuessCall({
				code:1002,
				desc:'商品id不能为空!',
			}));
			return false;
		};
		mysql(`select * from goods where g_id=${goodsId}`, ((data)=>{
			res.send(util.isScuessCall({
				data:data,
			}));
		}));
	}));

	/*新增商品信息接口
	
	 */
	app.all('/cms/api/goods/addOrUpdateGoods',((req, res)=>{
		if(!req.body.g_name){
			res.send(util.isScuessCall({
				code:1002,
				desc:'参数错误!',
			}));
			return false;
		};
		console.log(req.body);
		var json=req.body;
		var mysqlstr=null;
		if(req.body.type=='add'){
			mysqlstr=`insert into goods (g_name,g_image,b_id,t_id,g_market,g_price,g_place,g_stock,g_shelves,g_intro,g_detail) values 
				('${json.g_name}','${json.g_image}','${json.b_id}','${json.t_id}','${json.g_market}','${json.g_price}','${json.g_place}','${json.g_stock}','${json.g_shelves}','${json.g_intro}','${json.g_detail}')`;
		}else if(req.body.type=='update'){
			mysqlstr=`update goods set 
				g_name='${json.g_name}',
				g_image='${json.g_image}',
				b_id='${json.b_id}',
				t_id='${json.t_id}',
				g_market='${json.g_market}',
				g_price='${json.g_price}',
				g_place='${json.g_place}',
				g_stock='${json.g_stock}',
				g_shelves='${json.g_shelves}',
				g_intro='${json.g_intro}',
				g_detail='${json.g_detail}' where g_id=${json.g_id}`;
		};
		console.log(mysqlstr);
		mysql(mysqlstr, ((data)=>{
			res.send(util.isScuessCall({
				data:data,
			}));
		}));
		console.log(req.body);
	}));
	
};


	
